<template>
        <div class="module_box">

            <div class="module login h100">
                <div class="trapezoid">
                    <div class='l'></div>
                    <div class='center'>请登录</div><div class='r'>
                </div>
                </div>
                <ul>
                    <li class="tx"><span>
                <img :src=originTx alt="" class="file_tx">
            </span></li>
                    <li>
                        <div class="import">
                            <label for="">用户名:</label><br>
                            <input type="text" name="p_account" value=""  v-model="loginForm.p_account" @blur="getTx">
                        </div>
                    </li>
                    <li>
                        <div class="import">
                            <label for="">密&nbsp;&nbsp;码:</label><br>
                            <input type="password" name="p_password" value="" v-model="loginForm.p_password">
                        </div>
                    </li>
                    <li>
                        <div class="save">
                            <div class="saved">
                                <input type="checkbox" data-value="" name="remember" class="remember" id="exampleCheck1">
                                <label for="exampleCheck1">记住我 <span>不是自己的电脑请勿勾选！</span></label>
                            </div>
                            <div class="forget">忘记密码？</div>
                        </div>
                    </li>
                    <li>
                        <button @click="login">登录</button>
                    </li>
                </ul>
            </div>

        </div>
</template>

<script>
    import {mapMutations} from 'vuex'
    import $ from 'jquery'
    // import axios from 'axios'
    export default {
        name: "login",
        data(){
            return{
                loginForm: {
                    p_account: localStorage.getItem('p_account')?localStorage.getItem('p_account'):'',
                    p_password: localStorage.getItem('p_password')?localStorage.getItem('p_password'):''
                },
                originUrl:'http://127.0.0.1:3003',
                originTx:'https://6269-biyuwu-7gdk3rsjf4cd88b4-1300950401.tcb.qcloud.la/picture/1d623687e950352af5f7f3524443fbf2b3118b63.jpg?sign=9b1f623c0b8a936177e261ab45e602cc&t=1614480687',
                first:true,
            }
        },
        async created(){
            // await this.$cloudbase
            //     .auth({ persistence:"local"})
            //     .anonymousAuthProvider()
            //     .signIn();
            // console.log(this.$tcb)
            // console.log(this.$cloudbase.config.env)
             this.$cloudbase
                .database()
                .collection("typing")
                .get()
            // console.log(res)
            // console.log($())
            // $('.remember')[1].checked=true
            // await this.$cloudbase
            //     .auth({persistence: "local"})
            //     .anonymousAuthProvider()
            //     .signIn();
        },
        mounted(){
            localStorage.getItem('p_remember')?$('.remember')[0].checked=true:''
            // if ($('.remember')[0].checked) {
            //     this.originTx = '/uploads/user_information/'+this.loginForm.p_account+'.jpg'
            // }
        },
        methods:{
            ...mapMutations(['changeLogin']),
          async  getTx(){
                let _this =this;
                // let res='';
                await this.$cloudbase.database()
                  .collection('ykf_student')
                  .where({username:this.loginForm.p_account})
                  .get()
                  .then((res)=>{
                      _this.$cloudbase.getTempFileURL({
                          fileList:[res.data[0].Student_tx,"Student/tx"]
                      })
                          .then((res)=>{
                              // ls = res.fileList[1].tempFileURL
                              $('.file_tx').fadeToggle(300, function () {
                                  _this.originTx = res.fileList[1].tempFileURL
                                  $('.file_tx').fadeToggle(300)
                              })
                          })
                  }).catch(()=>{
                      _this.originTx='https://6269-biyuwu-7gdk3rsjf4cd88b4-1300950401.tcb.qcloud.la/picture/1d623687e950352af5f7f3524443fbf2b3118b63.jpg?sign=9b1f623c0b8a936177e261ab45e602cc&t=1614480687'
                  })
              //   this.$axios({
              //     url:'getTx',
              //     method:'get',
              //     params: _this.loginForm
              // }).then(res=>{
              //     let ls = res.data.p_head ? res.data.p_head : '/uploads/user_information/tx.jpg';
              //     if (ls!==_this.originTx) {
              //         $('.file_tx').fadeToggle(300, function () {
              //             _this.originTx = ls
              //             $('.file_tx').fadeToggle(300)
              //         })
              //     }
              // })
            },
            async login(){
                if (!this.first) return;
                let _this = this;
                if (this.loginForm.p_account===''||this.loginForm.p_password===''){
                    this.$message({
                        message: '账号和密码不能为空！',
                        type: 'error'
                    })
                }else{
                  let res= await _this.$cloudbase
                      .database()
                      .collection("ykf_student")
                      .where({username:_this.loginForm.p_account,password:_this.loginForm.p_password})
                      .get()
                    // console.log(res)
                    if (res.data.length!==0){
                        this.first=false
                        this.$message({
                            message: '登录成功！',
                            type: 'success'
                        })
                        localStorage.setItem('p_student_id',res.data[0]._id)
                        localStorage.setItem('p_account',this.loginForm.p_account)
                                    if ($('.remember')[0].checked){
                                        localStorage.setItem('p_password',this.loginForm.p_password)
                                        localStorage.setItem('p_remember','true')
                                    }else{
                                        localStorage.setItem('p_password','')
                                        localStorage.setItem('p_remember','false')
                                    }
                        _this.changeLogin({Authorization:res.data[0].Student_number})

                        // await this.$cloudbase
                        //     .auth({ persistence:"local"})
                        //     .anonymousAuthProvider()
                        //     .signIn();

                        let student_id = localStorage.getItem('p_student_id')
                        let student_information = await this.$cloudbase.database()
                            .collection('ykf_student')
                            .doc(student_id)
                            .get()

                        this.$store.dispatch('updateAdminFun', student_information.data[0].admin)

                        student_information = student_information.data[0]
                        // console.log(student_information)
                        await this.$cloudbase.getTempFileURL({
                            fileList:[student_information.Student_tx,'cloud://user/tx']
                        }).then((res)=>{
                            // console.log(res)
                            this.$store.dispatch('updateTxFun',res.fileList[1].tempFileURL)
                        })
                        this.$router.push('/home')
                        // _this.userToken='Bearer'+res.data[0].name;
                        // this.$router.push({name:'home'})

                    }else {
                        this.$message({
                            message: '账号或密码错误！',
                            type: 'error'
                        })
                    }

                //     this.$axios({
                //         method:'post',
                //         url:'/loginCheck',
                //         data:_this.loginForm,
                //     }).then(res=>{
                //         if (res.data==''){
                //             alert('账号或密码错误')
                //         } else{
                //             alert('登陆成功')
                //             localStorage.setItem('p_account',this.loginForm.p_account)
                //             if ($('.remember')[0].checked){
                //                 localStorage.setItem('p_password',this.loginForm.p_password)
                //                 localStorage.setItem('p_remember','true')
                //             }else{
                //                 localStorage.setItem('p_password','')
                //                 localStorage.setItem('p_remember','false')
                //             }
                //             this.$router.push('/index')
                //             _this.userToken='Bearer'+res.data.p_account;
                //             _this.changeLogin({Authorization:_this.userToken})
                //         }
                //     }).catch(error=>{
                //         console.log(error)
                //     })
                }
            }
        }
    }
</script>

<style scoped>

</style>